home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Help, I think with an array
- Date: 9 Apr 1996 10:03:44 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4ke59gINNkpi@mayne.ugrad.cs.ubc.ca>
- References: <4kbhcl$p4l@uwm.edu> <3169B805.8BE@willows.com>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <3169B805.8BE@willows.com>,
- Tarang Deshpande <tarang@willows.com> wrote:
- >Mario David Uy wrote:
- >>
- >> I am a beginner to programming and I have a problem.
- >> Well, I have this one table that I want to use. I looks somewhat like this.
- >>
- >> Age points
- >> 30 -3
- >> 31 -2
- >> 32-33 -1
- >> 34 0
- >> 35-36 1
-
- [ snip ]
-
-
- >Yes you should be using an array to store the table data but no if...else.
- >Instead of if...else you should use a loop through the array. If you tell me more
- >information like are the tables of the same size, are the age values in all the
- >tables the same, etc. Then I can post a more detailed answer.
-
- Why should he loop through the array? I think he wants a direct lookup:
-
- #include <stdio.h>
-
- static int points[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 ... 9 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 ... 19 */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 ... 29 */
- -3, -2, -1, -1, 0, 1, 1, 0, 0, 0, /* 30 ... 39 */
- };
-
- #define MAXAGE (sizeof(points)/sizeof(points[0])-1)
-
- int main()
-
- {
- unsigned int age;
-
- do {
- printf("Enter age:\n");
- scanf("%u",&age);
- } while (age > MAXAGE);
-
- printf("points associated with age %d = %d\n",age,points[age]);
-
- return 0;
- }
-
- --
-
-